Use itextsharp to modify PDF files (2)

Source: Internet
Author: User

Link: Use itextsharp to modify a PDF file (1)

ThisItextsharpIt is indeed a good thing to create and readPDFAlthough my requirements are relatively simple, I have read the relevant documents basically and completely. If I do not like English comrades, I can search for an articleC #ProductionPDFFile full strategy (xing Anting), this article ArticleYes Sir in useItextsharpAlthough the focus is on how to createPDFFile, read and modifyPDFThe file method is omitted. Therefore, it does not play much role for my task, but here, I still feel Thank you! Your selfless dedication.

 UsageItextsharpI will not elaborate on the method here, because it is very simple. Take a closer look at its documentation, it should be easy to create and readPDFFile.Here I will only talk about some of the problems I encountered during the use process, so that less people may take some detours:

1,PDFTheoretically, files cannot be modified after they are created successfully.

Because I need to modify the originalPDFFile, remove its header and footer, and replace it with a new header and footer. Therefore, I have studied how to retrieve only the content area of the original file (a rectangular area with the header, footer removed, left fixed area, and right fixed area removed) for a long time. TheGetimportedpageMethod to obtain the string, and then analyze the string (which is an extremely rough analysis, becausePDFThere are too many symbols in the file format, which will be described later), remove unnecessary parts, save the remaining parts, and generate a newPDFFile.

In theory, this method is correct and is also in line with our general logic thinking (because we,ProgramIn most cases, a similar method is used, such as decrypting a program ). I did get a newPDFFile, but then we found that this method is not universal, that is, it is effective for a file, but it may cause format mismatch for another file.

Because AnalysisPDFFile format is a very troublesome task. Many bytes are clearly in the content area but are displayed in the header, if I analyze the smallest and every flag in it, I 'd better look at it directly.SDKIn this case, the program will not be completed within the specified time.

 Solution:

I first studiedAcrobatInCropWhy can it be precisely tailored?

It turned out to make me dumb.CropIt is not a real cut, but it just blocks the area that needs to be cut out.CropIn the upper, lower, left, and right settings. It seems that the cropped area is still displayed.

Okay, now I have a bottom in my mind. I probably know how to do it. Now let's take a closer look.ItextsharpThe following section was not noticed before:

If you have an existing PDF file that represents a form, you can copy the pages of this form and paint text at precise locations on this form. you can't edit an existing PDF document, by saying: for instance Replace the word louagie by lowagie. to achieve this, You wowould have to know the exact location of the word louagie, paint a White Rectangle over it and paint the word lowagie on this white rectangle. please avoid this kind of 'patch 'work. do your PDF editing with an Adobe product.

Well, just like I thought, we used a new area to overwrite the area to be tailored.

This is much easier.ItextsharpOfTemplateFunction, put the text, images, and tables you needTemplateAnd thenTemplateAdd it to the appropriate location.

Oh, don't forget.TemplateAdd a White Rectangle to the bottom layer.

Note: As mentioned abovePDFFile format, in factPDFThe file format is very interesting. For more information, seePDFFile structure analysis and other articles. Otherwise, when you see<BT>,<Et>,/F1,TFYou will feel helpless.

2,PDFThe attributes in the file are not the attributes of the file in the general sense.

This made me take a detour.ItextsharpInDocument. opent ()Previously, related attributes were set, such:Subject/author/TitleWait, But the strange thing is that a newPDFIn the file, I usually use the method of viewing the attributes of a file, but do not see the expected attributes, are empty.

Later, after reminders from experienced colleagues, I learned:PDFFile properties areAcrobat ReaderCan be seen in a menu.

Haha, I usedAcrobatBasically, only files are converted to each other. There are no other functions, and there is no experience.

Although there have been countless attempts and numerous retries. This small program was completed in three days and played its due role. Post a page:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.