要使用Java代码,应该调用iText库。
要使用C#代码,应该调用iTextSharp库。
Here's the code to replace images in PDFs, in Java and C#. It will replace the first image in the first page.
Java代码
C#代码:
要使用C#代码,应该调用iTextSharp库。
Here's the code to replace images in PDFs, in Java and C#. It will replace the first image in the first page.
Java代码
PdfReader pdf =
new PdfReader(
"in.pdf");
PdfStamper stp = new PdfStamper(pdf, new FileOutputStream( "c:\\out.pdf"));
PdfWriter writer = stp.getWriter();
Image img = Image.getInstance( "image.png");
PdfDictionary pg = pdf.getPageN(1);
PdfDictionary res =
(PdfDictionary)PdfReader.getPdfObject(pg.get(PdfName.RESOURCES));
PdfDictionary xobj =
(PdfDictionary)PdfReader.getPdfObject(res.get(PdfName.XOBJECT));
if (xobj != null) {
for (Iterator it = xobj.getKeys().iterator(); it.hasNext(); ) {
PdfObject obj = xobj.get((PdfName)it.next());
if (obj.isIndirect()) {
PdfDictionary tg = (PdfDictionary)PdfReader.getPdfObject(obj);
PdfName type =
(PdfName)PdfReader.getPdfObject(tg.get(PdfName.SUBTYPE));
if (PdfName.IMAGE.equals(type)) {
PdfReader.killIndirect(obj);
Image maskImage = img.getImageMask();
if (maskImage != null)
writer.addDirectImageSimple(maskImage);
writer.addDirectImageSimple(img, (PRIndirectReference)obj);
break;
}
}
}
}
stp.close();
PdfStamper stp = new PdfStamper(pdf, new FileOutputStream( "c:\\out.pdf"));
PdfWriter writer = stp.getWriter();
Image img = Image.getInstance( "image.png");
PdfDictionary pg = pdf.getPageN(1);
PdfDictionary res =
(PdfDictionary)PdfReader.getPdfObject(pg.get(PdfName.RESOURCES));
PdfDictionary xobj =
(PdfDictionary)PdfReader.getPdfObject(res.get(PdfName.XOBJECT));
if (xobj != null) {
for (Iterator it = xobj.getKeys().iterator(); it.hasNext(); ) {
PdfObject obj = xobj.get((PdfName)it.next());
if (obj.isIndirect()) {
PdfDictionary tg = (PdfDictionary)PdfReader.getPdfObject(obj);
PdfName type =
(PdfName)PdfReader.getPdfObject(tg.get(PdfName.SUBTYPE));
if (PdfName.IMAGE.equals(type)) {
PdfReader.killIndirect(obj);
Image maskImage = img.getImageMask();
if (maskImage != null)
writer.addDirectImageSimple(maskImage);
writer.addDirectImageSimple(img, (PRIndirectReference)obj);
break;
}
}
}
}
stp.close();
C#代码:
PdfReader pdf =
new PdfReader(
"in.pdf");
PdfStamper stp = new PdfStamper(pdf, new FileStream( "out.pdf",
FileMode.Create));
PdfWriter writer = stp.Writer;
Image img = Image.GetInstance( "image.png");
PdfDictionary pg = pdf.GetPageN(1);
PdfDictionary res =
(PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
PdfDictionary xobj =
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
if (xobj != null) {
foreach (PdfName name in xobj.Keys) {
PdfObject obj = xobj.Get(name);
if (obj.IsIndirect()) {
PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
PdfName type =
(PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
if (PdfName.IMAGE.Equals(type)) {
PdfReader.KillIndirect(obj);
Image maskImage = img.ImageMask;
if (maskImage != null)
writer.AddDirectImageSimple(maskImage);
writer.AddDirectImageSimple(img, PRIndirectReference)obj);
break;
}
}
}
}
stp.Close();
PdfStamper stp = new PdfStamper(pdf, new FileStream( "out.pdf",
FileMode.Create));
PdfWriter writer = stp.Writer;
Image img = Image.GetInstance( "image.png");
PdfDictionary pg = pdf.GetPageN(1);
PdfDictionary res =
(PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
PdfDictionary xobj =
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
if (xobj != null) {
foreach (PdfName name in xobj.Keys) {
PdfObject obj = xobj.Get(name);
if (obj.IsIndirect()) {
PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
PdfName type =
(PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
if (PdfName.IMAGE.Equals(type)) {
PdfReader.KillIndirect(obj);
Image maskImage = img.ImageMask;
if (maskImage != null)
writer.AddDirectImageSimple(maskImage);
writer.AddDirectImageSimple(img, PRIndirectReference)obj);
break;
}
}
}
}
stp.Close();
本文转自 h2appy 51CTO博客,原文链接:http://blog.51cto.com/h2appy/149127,如需转载请自行联系原作者