I am trying to draw an XObject from type form in a new PDF page, I did the following:
this.cell = (PDFormXObject) xobject;
PDDocument document = new PDDocument();
PDPage page = new PDPage();
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.transform(new Matrix(1, 0, 0, 1, -cell.getBBox().getLowerLeftX(),-cell.getBBox().getLowerLeftY()));
contentStream.drawForm(cell);
contentStream.close();
document.addPage(page);
document.save("D://temp//blankPage.pdf");
document.close();
The result is as desired, but the xobject has larger border with empty areas, I only need to isolate the internal box with (which I add yellow border to it), here is a ss:
Related
I'm working in some Annotation stuff with iText.
I tried to draw a Stamp Annotation with text on an existing PDF file.
The Stamp Annotation did show on the PDF but when I moving the stamp (or resizing), the text (will all format) is disappear and can't gain it back anymore.
Here is my code
PdfReader reader = new PdfReader(SRC_FILE);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(DEST_FILE));
PdfWriter writer = stamper.getWriter();
BaseColor textColor = BaseColor.ORANGE;
BaseColor fillColor = BaseColor.GRAY;
Rectangle rectangle = new Rectangle(150, 250, 450, 320);
PdfAnnotation annotation = PdfAnnotation.createSquareCircle(writer, rectangle, null, true);
annotation.setColor(textColor);
PdfDictionary dict = new PdfDictionary();
PdfTemplate template = PdfTemplate.createTemplate(writer, rectangle.getWidth(), rectangle.getHeight());
template.setBoundingBox(rectangle);
template.setColorFill(fillColor);
//template.setColorStroke(textColor);
template.setLineWidth(4);
template.rectangle(rectangle.getLeft(), rectangle.getBottom(), rectangle.getWidth(), rectangle.getHeight());
template.closePathFillStroke();
template.setColorFill(textColor);
ColumnText columnText = new ColumnText(template);
columnText.setSimpleColumn(rectangle);
Paragraph p = new Paragraph("This is my test", new Font(Font.FontFamily.HELVETICA, 24, Font.BOLD));
p.setAlignment(Element.ALIGN_CENTER);
p.setLeading(50);
columnText.setAlignment(Element.ALIGN_CENTER);
columnText.addElement(p);
columnText.go();
writer.releaseTemplate(template);
dict.put(PdfName.N, template.getIndirectReference());
annotation.put(PdfName.AP, dict);
annotation.put(PdfName.F, new PdfNumber(4));
stamper.addAnnotation(annotation, 1);
stamper.close();
reader.close();
I attached the Screenshot this issue for your review too.
I did some research and if I change the line code
PdfAnnotation annotation = PdfAnnotation.createSquareCircle(writer, rectangle, null, true);
to
PdfAnnotation annotation= stamper.getWriter().createAnnotation(rectangle, PdfName.FREETEXT);
The text will still there when I moving the Stamp, but still disappear if I resize the Rectangle.
I am just fiddeling around with Flying Saucer to generate a PDF from a plain HTML Template I wrote.
Unfortunately, flying saucer seems to generate 1/2 empty page on my first page so the main content actually just begins on the second half of the first page.
There is really not much to show as I am working with a normal html page that lies in a String Object consisting of some table and well-formed html. Displaying the html shows exactly what I want and the part that gets rendered does also look great and as expected - but there is half an empty page...
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer;
PdfImportedPage page;
byte[] pdfDaten;
writer = PdfWriter.getInstance(document, baos);
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfReader pdfPage = pdfBuilder.doRenderGeneratorToPDF(html_page);
document.setPageSize(PageSize.A4);
page = writer.getImportedPage(pdfPage, 1);
document.newPage();
cb.addTemplate(page, 0, 0);
document.close();
baos.flush();
pdfDaten = baos.toByteArray();
baos.close();
return pdfDaten;
Did anybody else experience this problem and has a hint or solution?
I tried to overlap a model pdf, to see the differences between that and my new pdf.
here is my code:
PdfReader reader = new PdfReader(WorkDir + "\\model.pdf");
PdfImportedPage page = writer.GetImportedPage(reader, 1);
pdfOut.NewPage();
if (checkBox1.Checked) writer.DirectContent.AddTemplate(page, 0, 0);
I only want to put the page only if I check the checkbox1.
But if checkbox1 is not checked, the outout pdf file is very large and the overlapped file is not visible.
I removed the overlap part:
PdfReader reader = new PdfReader(WorkDir + "\\model.pdf");
//PdfImportedPage page = writer.GetImportedPage(reader, 1);
pdfOut.NewPage();
//if (checkBox1.Checked) writer.DirectContent.AddTemplate(page, 0, 0);
and the file size is now ok.
What am I doing wrong?
I think that the DirectContent element adds the page, but is not visible.
That can explain why the output file is so big( with overlap part file size is 700KB, without only 4KB)
The data of the page are already added to the new document when importing the page
PdfImportedPage page = writer.GetImportedPage(reader, 1);
The later writer.DirectContent.AddTemplate(page, 0, 0); only makes it visible.
Thus, you might want
PdfReader reader = new PdfReader(WorkDir + "\\model.pdf");
pdfOut.NewPage();
if (checkBox1.Checked)
{
PdfImportedPage page = writer.GetImportedPage(reader, 1);
writer.DirectContent.AddTemplate(page, 0, 0);
}
I want to write line by line on a pdf document
the code I have is writing the text in the center of the page
how can I write line by line?
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
// Create a font
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.TopCenter);
With new XRect(0, 0, page.Width, page.Height) you specify where text will be drawn.
Use a smaller rectangle and increase the second value from line to line.
PDFsharp includes several examples:
http://pdfsharp.net/wiki/PDFsharpSamples.ashx
Especially check Text Layout. Sample code included with the source package of PDFsharp.
Also check out MigraDoc as it adds pagebreaks automatically.
http://pdfsharp.net/wiki/MigraDocSamples.ashx
I am able to insert an Image inside an existing pdf document, but the problem is,
The image is placed at the bottom of the page
The page becomes white with the newly added text showing on it.
I am using following code.
List<PDPage> pages = pdDoc.getDocumentCatalog().getAllPages();
if(pages.size() > 0){
PDJpeg img = new PDJpeg(pdDoc, in);
PDPageContentStream stream = new PDPageContentStream(pdDoc,pages.get(0));
stream.drawImage(img, 60, 60);
stream.close();
}
I want the image on the first page.
PDFBox is a low-level library to work with PDF files. You are responsible for more high-level features. So in this example, you are placing your image at (60, 60) starting from lower-left corner of your document. That is what stream.drawImage(img, 60, 60); does.
If you want to move your image somewhere else, you have to calculate and provide the wanted location (perhaps from dimensions obtained with page.findCropBox(), or manually input your location).
As for the text, PDF document elements are absolutely positioned. There are no low-level capabilities for re-flowing text, floating or something similar. If you write your text on top of your image, it will be written on top of your image.
Finally, for your page becoming white -- you are creating a new content stream and so overwriting the original one for your page. You should be appending to the already available stream.
The relevant line is:
PDPageContentStream stream = new PDPageContentStream( pdDoc, pages.get(0));
What you should do is call it like this:
PDPageContentStream stream = new PDPageContentStream( pdDoc, pages.get(0), true, true);
The first true is whether to append content, and the final true (not critical here) is whether to compress the stream.
Take a look at AddImageToPDF sample available from PDFBox sources.
Try this
doc = PDDocument.load( inputFileName );
PDXObjectImage ximage = null;
ximage = new PDJpeg(doc, new FileInputStream( image )
PDPage page = (PDPage)doc.getDocumentCatalog().getAllPages().get(0);
PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
contentStream.drawImage( ximage, 425, 675 );
contentStream.close();
This prints the image in first page. If u want to print in all pages just put on a for loop with a condition of number of pages as the limit.
This worked for me well!
So late answer but this is for who works on it in 2020 with Kotlin: drawImage() is getting float values inside itself so try this:
val file = File(getPdfFile(FILE_NAME))
val document = PDDocument.load(file)
val page = document.getPage(0)
val contentStream: PDPageContentStream
contentStream = PDPageContentStream(document, page, true, true)
// Define a content stream for adding to the PDF
val bitmap: Bitmap? = ImageSaver(this).setFileName("sign.png").setDirectoryName("signature").load()
val mediaBox: PDRectangle = page.mediaBox
val ximage: PDImageXObject = JPEGFactory.createFromImage(document, bitmap)
contentStream.drawImage(ximage, mediaBox.width - 4 * 65, 26f)
// Make sure that the content stream is closed:
contentStream.close()
// Save the final pdf document to a file
pdfSaveLocation = "$directoryPDF/$UPDATED_FILE_NAME"
val pathSave = pdfSaveLocation
document.save(pathSave)
document.close()
I am creating a new PDF and running below code in a loop - to add one image per page and below co-ordinates and height and width values work well for me.
where out is BufferedImage reference variable
PDPage page = new PDPage();
outputdocument.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(outputdocument, page, AppendMode.APPEND, true);
PDImageXObject pdImageXObject = JPEGFactory.createFromImage(outputdocument, out);
contentStream.drawImage(pdImageXObject, 5, 2, 600, 750);
contentStream.close();
This link gives you details about Class PrintImageLocations.
This PrintImageLocations will give you the x and y coordinates of the images.
Usage: java org.apache.pdfbox.examples.util.PrintImageLocations input-pdf