Why the ordinate of SetFixedPosition() in Itext7 .NET not work well in Web Application? - pdf

I used itext7 .NET to make a demo of Console project to add text into an existing pdf. the code is below:
PdfDocument pdfTemple = new PdfDocument(new PdfReader(templateFile), new PdfWriter(templeFile));
Document documentTemple = new Document(pdfTemple, PageSize.A4);
Text text = new Text(string.Format(#"{0} / {1} ", month, year))
.SetBackgroundColor(ColorConstants.WHITE)
.SetBold()
.SetFontSize(11);
documentTemple.Add(new Paragraph(text).SetFixedPosition(1, 424, 740, 60));
text = new Text(DateTime.Now.ToString("MM/dd/yyy"))
.SetBackgroundColor(ColorConstants.WHITE)
//.SetBold()
.SetFontSize(10);
documentTemple.Add(new Paragraph(text).SetFixedPosition(1, 503, 710, 60));
documentTemple.Close();
It works well in the Console project demo. But when I use the same code in a web application (MVC, .NET4.7.2), it doesn't work. Only the text with the ordinate between 50 - 350 can display on the created pdf file.
I need to add text to the position on the pdf file with the ordinate between 40 and 740. How can I let it work in my web application (MVC, .NET4.7.2)?
Thanks.
additions:
I found that if I open the template pdf (I write text into it) with photoshop, I will get such screen:
If I write text into the white area shown in the screenshot above, the text will not show on the pdf file. but if I write text into another area, the text will show on the pdf file.

Related

How do I use iTextSharp (or iText) to crop and copy a page from one PDF to another

I've written code to do the following:
Take a PDF of a certain page size (e.g., 8.5" x 11")
Create a new PDF with a larger page size (e.g., 17" x 11")
Impose the original PDF onto the new one (e.g., 2-up such that the resulting new PDF has the original PDF side-by-side)
To do this, I use the PdfWriter.GetImportedPage method to get the current page from the original PDF, then use the PdfContentByte.AddTemplate(page, x, y) method to place the original page onto the current page of the new PDF.
My new challenge is that I need to crop the original PDF before adding it to the new PDF. For example, imagine I want to crop 2" off of the original PDF before imposing it. The input PDF would still be 8.5" x 11" and the new PDF would still be 17" x 11", but the two "copies" of the original PDF in the new one would have had 2" removed from its top, right, bottom and left sides.
Hopefully these images can make this clearer. Here's what I have now, doing a 2-up imposition. (This is working swimmingly.)
But here's what I need to do:
I know that I can alter the display of the PDF in a viewer by using the MediaBox or CropBox settings, but those settings aren't respected by AddTemplate. I know that with AddTemplate I can use a transform matrix to position the page or to scale or rotate it, but I don't want to shrink the original PDF, I want to crop it.
Thanks
I found that I can use the BoundingBox of the imported page to crop it prior to adding it to the new PDF (via AddTemplate).
So my code looks something like this:
PdfImportedPage page = writer.GetImportedPage(pageNumber);
// Crop!
page.BoundingBox = new Rectangle(llx, lly, urx, ury);
// Add to new PDF
writer.DirectContent.AddTemplate(page, x, y);
That does the trick!

Lost some text when extracting pdf

I've tried to get all the text on the page by using iText, but I have no idea why every coordinate text loses the last two character.
PdfDocument pdfDoc = new PdfDocument(new PdfReader(#"E:\Coding\COOR.pdf"));
LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
PdfCanvasProcessor parser = new PdfCanvasProcessor(strategy);
parser.ProcessPageContent(pdfDoc.GetFirstPage());
Console.Write(strategy.GetResultantText());
pdfDoc.Close();
Console.WriteLine("Great!");
Console.ReadKey();
You can also download my code from
https://1drv.ms/u/s!Al1hUSZtR4OjwU3XVBRQGneVaZlS
In short
The reason for that "lost text" is that the missing "text" isn't there to start with!
In detail
The contents of you PDF file are constructed in a misleading manner.
On the one hand there are very many path definitions which then are stroked (drawn). These drawings create what you can see in a viewer, both text and table lines.
On the other hand there are a few text drawing instructions to draw text using text rendering mode 3 which is... invisible! These drawings create the text you can copy&paste in a viewer or extract using iText.
Unfortunately the text in the text drawing instructions and the text drawn using paths does not match completely. The text you retrieve via copy&paste or text extraction, therefore, differs from your expectations.
Also the glyph sizes and positions are not exactly the same
To illustrate this I made the text drawing instructions use the normal (fill) text rendering mode. The top left corner which originally looks like this:
with that change looks like this:
As you see the formerly invisible text is only approximately at the same position as the visible drawings, and it is somewhat broken: The symbol for degrees is weirdly represented as "¡ã", and the longitude fractional seconds and the following symbol for seconds are missing.
To correctly extract the originally visible data, you'll need to use OCR instead of text extraction.

Change font size in text box - apache poi word docx

I found the answer that explains how to insert a new text box into docx document.
create text box in document .docx using apache poi
The problem is that I cannot change the font size inside a newly created text box.
Does anyone know how to do that?
Reference : create text box in document .docx using apache poi
The ctTxbxContent.addNewP() in my code creates a CTP object. The XWPFParagraph has a constructor XWPFParagraph(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP prgrph, IBody part). So you can get a XWPFParagraph from the CTP object and then use the default apache-poi methods further.
...
CTTxbxContent ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent();
XWPFParagraph textboxparagraph = new XWPFParagraph(ctTxbxContent.addNewP(), (IBody)doc);
XWPFRun textboxrun = textboxparagraph.createRun();
textboxrun.setText("The TextBox text...");
textboxrun.setFontSize(24);
...

How to draw Matrix Code with PdfSharp?

I need to make a PDF report via PdfSharp. The report must include a QRCode, or data matrix code, but I can't seem to be able to draw it on the page.
The values it's asking for are value as String and length as Integer so here's what I'm doing:
Dim myNewCode As New PdfSharp.Drawing.BarCodes.CodeDataMatrix("1234567890", 10)
Then I try to draw it:
gfx.DrawMatrixCode(myNewCode, myXPoint)
It asks for an XPoint location so I set it to this:
Dim myXPoint As New XPoint(500,500)
Which only needs values for x and y.
It compiles OK but when I try to open the file I get the next error
An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem
My Acrobat version is 11.0.5, and there is no problem opening other PDF files which already contain these kind of codes.
Specify the size to get a correct PDF file:
var myXSize = new XSize(100, 100);
var myNewCode = new PdfSharp.Drawing.BarCodes.CodeDataMatrix("1234567890", 10, myXSize);
var myXPoint = new XPoint(200, 300);
gfx.DrawMatrixCode(myNewCode, myXPoint);
Please note that due to legal reasons, the open source version of PDFsharp does not include the implementation of the Data Matrix Code and shows dummy images instead.
Another option would be to use a 3rd party library (ZXing) to generate the QR Code bitmap and draw it as a bitmap with DrawImage() on the PDF.

How to automate extracting pages from a PDF using AppleScript and Acrobat Pro?

I'm new to AppleScript, but I am trying to create a script that will go through all PDFs in a folder extracting the pages into separate files. My plan is to use a combination of Automator and AppleScript.
My AppleScript so far is:
tell application "Adobe Acrobat Pro"
open theFile
set numPages to (count active doc each page)
--execute the extraction here
end tell
The command in Acrobat Pro is under Options > Extract Pages..., where I can specify the page range and to extract to separate files. However, I can't seem to find a way to do this with the Acrobat Pro Dictionary in AppleScript.
There is an execute command that executes a menu item, but I can't seem to get it working (I'm also not sure of the syntax to use; i.e. execute "Options:Extract Pages..."?). Any help on this?
I think you can do this entirely with the Automator without the need for AppleScript or Adobe software. The "PDF to Images" action splits a multi-page .PDF file into individual .PDF files, one per page:
You can use Adobe Acrobat Pro. Here is an example using Adobe Acrobat Pro XI. It uses Acrobat's "Actions" (previously called "Batch Processing") with custom JavaScript.
Adobe Acrobat Pro - Edit Action
You can create a new action that prompts the user to select a folder of pdf files to process. Then you can add JavaScript execution that searches for the pdf file names and extractPages function to extract all the pages from a PDF
Adobe Acrobat Pro - JavaScript
The following will extract all of the pages into separate PDFs. It appends a suffix on the end with each sheet number. It pads the sheet number with zeros based on the method described in the links which basically adds a bunch of zeros in front and then only slices out to take however many last digits of the string depending on how many sheets you typically have.
/* Extract Pages to Folder */
var re = /.*\/|\.pdf$/ig;
var filename = this.path.replace(re,"");
{
for ( var i = 0; i < this.numPages; i++ )
this.extractPages
({
nStart: i,
nEnd: i,
cPath : filename + "_s" + ("000000" + (i+1)).slice (-3) + ".pdf"
});
};
References
JavaScript for Acrobat API Reference > JavaScript API > Doc > Doc methods > extractPages
Extract pages to separate pdf's (something wrong with loop?)
How can I create a Zerofilled value using JavaScript?
How to output integers with leading zeros in JavaScript [duplicate]