How to embed all fonts from other PDF iText 7











up vote
0
down vote

favorite
1












I am trying to overlay two PDF files using iText7/C#.
The first one is kind of background and the second one is containing form fields.
Everything works fine and only problem is that I lose fonts from the second file.



I try as follows:



static public bool Overlay(string back_path, string front_path, string merge_path)
{
PdfReader reader;
PdfDocument pdf = null, front;
try
{
reader = new PdfReader(back_path);
pdf = new PdfDocument(reader, new PdfWriter(merge_path));
front = new PdfDocument(new PdfReader(front_path));

var form = PdfAcroForm.GetAcroForm(front, false);
PdfAcroForm dform = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();

// copy styles
dform.SetDefaultResources(form.GetDefaultResources());
dform.SetDefaultAppearance(form.GetDefaultAppearance().GetValue());

// do overlay
foreach (KeyValuePair<string, PdfFormField> pair in fields)
{
try
{
var field = pair.Value;
PdfPage page = field.GetWidgets().First().GetPage();

int pg_no = front.GetPageNumber(page);
if (pg_no < front_start_page || pg_no > front_end_page)
continue;
PdfObject copied = field.GetPdfObject().CopyTo(pdf, true);
PdfFormField copiedField = PdfFormField.MakeFormField(copied, pdf);

// The following returns null. If it returns something, I think I could use copiedField.setFont(font).
// var font = field.GetFont();

dform.AddField(copiedField, pdf.GetPage(pg_no));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Overlaying field {pair.Key} failed. ({ex.Message})");
}
}

pdf.Close();
return true;
}
catch (Exception ex)
{
throw new OverlayException(ex.Message);
}
}

public static PdfDictionary get_font_dict(PdfDocument pdfDoc)
{
PdfDictionary acroForm = pdfDoc.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.AcroForm);
if (acroForm == null)
{
return null;
}
PdfDictionary dr = acroForm.GetAsDictionary(PdfName.DR);
if (dr == null)
{
return null;
}
PdfDictionary font = dr.GetAsDictionary(PdfName.Font);
return font;
}


So basically I get all fonts from the second PDF and copy them to the final PDF.
But it does not work.



Logically, I think setting font of the original field to the copied one is the right way.
I mean PdfFormField.GetFont() and SetFont().
But it always returns null.










share|improve this question




















  • 1




    Copying just PDF objects is not enough. You also have to reference them properly, which does not happen automatically. Show us your way of merging files and if possible example PDFs to reproduce the issue
    – Alexey Subach
    Nov 20 at 6:13










  • Hi, Alexey. Thanks for your reply. Please check the edit. I added as much as I can. Sorry, I can't add the example PDFs. Looking forward to your answer.
    – Piao David
    Nov 20 at 6:36










  • Does the background PDF contain any form fields or other annotations? If it does not, the most simple way to solve your issue is to add the background to the form field PDF instead of adding the form fields and global AcroForm information to the background PDF. Furthermore, does the form PDF contain any static content?
    – mkl
    Nov 20 at 11:02










  • Thanks, @mkl. I'll try your suggestion. For your information, the background PDF can be assumed not to have form fields or annotations. I mean we can assume background PDF only contains static content (scanned form) and the front PDF only contains formfields. What I'm most curious on is why field.GetFont() returns null. If not, it seems to be straightforward.
    – Piao David
    Nov 20 at 11:20

















up vote
0
down vote

favorite
1












I am trying to overlay two PDF files using iText7/C#.
The first one is kind of background and the second one is containing form fields.
Everything works fine and only problem is that I lose fonts from the second file.



I try as follows:



static public bool Overlay(string back_path, string front_path, string merge_path)
{
PdfReader reader;
PdfDocument pdf = null, front;
try
{
reader = new PdfReader(back_path);
pdf = new PdfDocument(reader, new PdfWriter(merge_path));
front = new PdfDocument(new PdfReader(front_path));

var form = PdfAcroForm.GetAcroForm(front, false);
PdfAcroForm dform = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();

// copy styles
dform.SetDefaultResources(form.GetDefaultResources());
dform.SetDefaultAppearance(form.GetDefaultAppearance().GetValue());

// do overlay
foreach (KeyValuePair<string, PdfFormField> pair in fields)
{
try
{
var field = pair.Value;
PdfPage page = field.GetWidgets().First().GetPage();

int pg_no = front.GetPageNumber(page);
if (pg_no < front_start_page || pg_no > front_end_page)
continue;
PdfObject copied = field.GetPdfObject().CopyTo(pdf, true);
PdfFormField copiedField = PdfFormField.MakeFormField(copied, pdf);

// The following returns null. If it returns something, I think I could use copiedField.setFont(font).
// var font = field.GetFont();

dform.AddField(copiedField, pdf.GetPage(pg_no));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Overlaying field {pair.Key} failed. ({ex.Message})");
}
}

pdf.Close();
return true;
}
catch (Exception ex)
{
throw new OverlayException(ex.Message);
}
}

public static PdfDictionary get_font_dict(PdfDocument pdfDoc)
{
PdfDictionary acroForm = pdfDoc.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.AcroForm);
if (acroForm == null)
{
return null;
}
PdfDictionary dr = acroForm.GetAsDictionary(PdfName.DR);
if (dr == null)
{
return null;
}
PdfDictionary font = dr.GetAsDictionary(PdfName.Font);
return font;
}


So basically I get all fonts from the second PDF and copy them to the final PDF.
But it does not work.



Logically, I think setting font of the original field to the copied one is the right way.
I mean PdfFormField.GetFont() and SetFont().
But it always returns null.










share|improve this question




















  • 1




    Copying just PDF objects is not enough. You also have to reference them properly, which does not happen automatically. Show us your way of merging files and if possible example PDFs to reproduce the issue
    – Alexey Subach
    Nov 20 at 6:13










  • Hi, Alexey. Thanks for your reply. Please check the edit. I added as much as I can. Sorry, I can't add the example PDFs. Looking forward to your answer.
    – Piao David
    Nov 20 at 6:36










  • Does the background PDF contain any form fields or other annotations? If it does not, the most simple way to solve your issue is to add the background to the form field PDF instead of adding the form fields and global AcroForm information to the background PDF. Furthermore, does the form PDF contain any static content?
    – mkl
    Nov 20 at 11:02










  • Thanks, @mkl. I'll try your suggestion. For your information, the background PDF can be assumed not to have form fields or annotations. I mean we can assume background PDF only contains static content (scanned form) and the front PDF only contains formfields. What I'm most curious on is why field.GetFont() returns null. If not, it seems to be straightforward.
    – Piao David
    Nov 20 at 11:20















up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I am trying to overlay two PDF files using iText7/C#.
The first one is kind of background and the second one is containing form fields.
Everything works fine and only problem is that I lose fonts from the second file.



I try as follows:



static public bool Overlay(string back_path, string front_path, string merge_path)
{
PdfReader reader;
PdfDocument pdf = null, front;
try
{
reader = new PdfReader(back_path);
pdf = new PdfDocument(reader, new PdfWriter(merge_path));
front = new PdfDocument(new PdfReader(front_path));

var form = PdfAcroForm.GetAcroForm(front, false);
PdfAcroForm dform = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();

// copy styles
dform.SetDefaultResources(form.GetDefaultResources());
dform.SetDefaultAppearance(form.GetDefaultAppearance().GetValue());

// do overlay
foreach (KeyValuePair<string, PdfFormField> pair in fields)
{
try
{
var field = pair.Value;
PdfPage page = field.GetWidgets().First().GetPage();

int pg_no = front.GetPageNumber(page);
if (pg_no < front_start_page || pg_no > front_end_page)
continue;
PdfObject copied = field.GetPdfObject().CopyTo(pdf, true);
PdfFormField copiedField = PdfFormField.MakeFormField(copied, pdf);

// The following returns null. If it returns something, I think I could use copiedField.setFont(font).
// var font = field.GetFont();

dform.AddField(copiedField, pdf.GetPage(pg_no));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Overlaying field {pair.Key} failed. ({ex.Message})");
}
}

pdf.Close();
return true;
}
catch (Exception ex)
{
throw new OverlayException(ex.Message);
}
}

public static PdfDictionary get_font_dict(PdfDocument pdfDoc)
{
PdfDictionary acroForm = pdfDoc.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.AcroForm);
if (acroForm == null)
{
return null;
}
PdfDictionary dr = acroForm.GetAsDictionary(PdfName.DR);
if (dr == null)
{
return null;
}
PdfDictionary font = dr.GetAsDictionary(PdfName.Font);
return font;
}


So basically I get all fonts from the second PDF and copy them to the final PDF.
But it does not work.



Logically, I think setting font of the original field to the copied one is the right way.
I mean PdfFormField.GetFont() and SetFont().
But it always returns null.










share|improve this question















I am trying to overlay two PDF files using iText7/C#.
The first one is kind of background and the second one is containing form fields.
Everything works fine and only problem is that I lose fonts from the second file.



I try as follows:



static public bool Overlay(string back_path, string front_path, string merge_path)
{
PdfReader reader;
PdfDocument pdf = null, front;
try
{
reader = new PdfReader(back_path);
pdf = new PdfDocument(reader, new PdfWriter(merge_path));
front = new PdfDocument(new PdfReader(front_path));

var form = PdfAcroForm.GetAcroForm(front, false);
PdfAcroForm dform = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();

// copy styles
dform.SetDefaultResources(form.GetDefaultResources());
dform.SetDefaultAppearance(form.GetDefaultAppearance().GetValue());

// do overlay
foreach (KeyValuePair<string, PdfFormField> pair in fields)
{
try
{
var field = pair.Value;
PdfPage page = field.GetWidgets().First().GetPage();

int pg_no = front.GetPageNumber(page);
if (pg_no < front_start_page || pg_no > front_end_page)
continue;
PdfObject copied = field.GetPdfObject().CopyTo(pdf, true);
PdfFormField copiedField = PdfFormField.MakeFormField(copied, pdf);

// The following returns null. If it returns something, I think I could use copiedField.setFont(font).
// var font = field.GetFont();

dform.AddField(copiedField, pdf.GetPage(pg_no));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Overlaying field {pair.Key} failed. ({ex.Message})");
}
}

pdf.Close();
return true;
}
catch (Exception ex)
{
throw new OverlayException(ex.Message);
}
}

public static PdfDictionary get_font_dict(PdfDocument pdfDoc)
{
PdfDictionary acroForm = pdfDoc.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.AcroForm);
if (acroForm == null)
{
return null;
}
PdfDictionary dr = acroForm.GetAsDictionary(PdfName.DR);
if (dr == null)
{
return null;
}
PdfDictionary font = dr.GetAsDictionary(PdfName.Font);
return font;
}


So basically I get all fonts from the second PDF and copy them to the final PDF.
But it does not work.



Logically, I think setting font of the original field to the copied one is the right way.
I mean PdfFormField.GetFont() and SetFont().
But it always returns null.







c# pdf fonts pdf-generation itext7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 6:35

























asked Nov 20 at 2:42









Piao David

157




157








  • 1




    Copying just PDF objects is not enough. You also have to reference them properly, which does not happen automatically. Show us your way of merging files and if possible example PDFs to reproduce the issue
    – Alexey Subach
    Nov 20 at 6:13










  • Hi, Alexey. Thanks for your reply. Please check the edit. I added as much as I can. Sorry, I can't add the example PDFs. Looking forward to your answer.
    – Piao David
    Nov 20 at 6:36










  • Does the background PDF contain any form fields or other annotations? If it does not, the most simple way to solve your issue is to add the background to the form field PDF instead of adding the form fields and global AcroForm information to the background PDF. Furthermore, does the form PDF contain any static content?
    – mkl
    Nov 20 at 11:02










  • Thanks, @mkl. I'll try your suggestion. For your information, the background PDF can be assumed not to have form fields or annotations. I mean we can assume background PDF only contains static content (scanned form) and the front PDF only contains formfields. What I'm most curious on is why field.GetFont() returns null. If not, it seems to be straightforward.
    – Piao David
    Nov 20 at 11:20
















  • 1




    Copying just PDF objects is not enough. You also have to reference them properly, which does not happen automatically. Show us your way of merging files and if possible example PDFs to reproduce the issue
    – Alexey Subach
    Nov 20 at 6:13










  • Hi, Alexey. Thanks for your reply. Please check the edit. I added as much as I can. Sorry, I can't add the example PDFs. Looking forward to your answer.
    – Piao David
    Nov 20 at 6:36










  • Does the background PDF contain any form fields or other annotations? If it does not, the most simple way to solve your issue is to add the background to the form field PDF instead of adding the form fields and global AcroForm information to the background PDF. Furthermore, does the form PDF contain any static content?
    – mkl
    Nov 20 at 11:02










  • Thanks, @mkl. I'll try your suggestion. For your information, the background PDF can be assumed not to have form fields or annotations. I mean we can assume background PDF only contains static content (scanned form) and the front PDF only contains formfields. What I'm most curious on is why field.GetFont() returns null. If not, it seems to be straightforward.
    – Piao David
    Nov 20 at 11:20










1




1




Copying just PDF objects is not enough. You also have to reference them properly, which does not happen automatically. Show us your way of merging files and if possible example PDFs to reproduce the issue
– Alexey Subach
Nov 20 at 6:13




Copying just PDF objects is not enough. You also have to reference them properly, which does not happen automatically. Show us your way of merging files and if possible example PDFs to reproduce the issue
– Alexey Subach
Nov 20 at 6:13












Hi, Alexey. Thanks for your reply. Please check the edit. I added as much as I can. Sorry, I can't add the example PDFs. Looking forward to your answer.
– Piao David
Nov 20 at 6:36




Hi, Alexey. Thanks for your reply. Please check the edit. I added as much as I can. Sorry, I can't add the example PDFs. Looking forward to your answer.
– Piao David
Nov 20 at 6:36












Does the background PDF contain any form fields or other annotations? If it does not, the most simple way to solve your issue is to add the background to the form field PDF instead of adding the form fields and global AcroForm information to the background PDF. Furthermore, does the form PDF contain any static content?
– mkl
Nov 20 at 11:02




Does the background PDF contain any form fields or other annotations? If it does not, the most simple way to solve your issue is to add the background to the form field PDF instead of adding the form fields and global AcroForm information to the background PDF. Furthermore, does the form PDF contain any static content?
– mkl
Nov 20 at 11:02












Thanks, @mkl. I'll try your suggestion. For your information, the background PDF can be assumed not to have form fields or annotations. I mean we can assume background PDF only contains static content (scanned form) and the front PDF only contains formfields. What I'm most curious on is why field.GetFont() returns null. If not, it seems to be straightforward.
– Piao David
Nov 20 at 11:20






Thanks, @mkl. I'll try your suggestion. For your information, the background PDF can be assumed not to have form fields or annotations. I mean we can assume background PDF only contains static content (scanned form) and the front PDF only contains formfields. What I'm most curious on is why field.GetFont() returns null. If not, it seems to be straightforward.
– Piao David
Nov 20 at 11:20














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










In a comment you clarified:




the background PDF can be assumed not to have form fields or annotations. I mean we can assume background PDF only contains static content (scanned form) and the front PDF only contains formfields.




In that case the easiest way to implement your method is to add the background as xobject to the form PDF instead of adding the form to the background PDF.



You can simply do that like this:



PdfReader formReader = new PdfReader(front_path);
PdfReader backReader = new PdfReader(back_path);
PdfWriter writer = new PdfWriter(merge_path);

using (PdfDocument source = new PdfDocument(backReader))
using (PdfDocument target = new PdfDocument(formReader, writer))
{
PdfFormXObject xobject = source.GetPage(1).CopyAsFormXObject(target);
PdfPage targetFirstPage = target.GetFirstPage();
PdfStream stream = targetFirstPage.NewContentStreamBefore();
PdfCanvas pdfCanvas = new PdfCanvas(stream, targetFirstPage.GetResources(), target);
Rectangle cropBox = targetFirstPage.GetCropBox();
pdfCanvas.AddXObject(xobject, cropBox.GetX(), cropBox.GetY());
}


Depending on the exact static contents of the background and the form PDF, you might want to use NewContentStreamAfter instead of NewContentStreamBefore or even to use some nifty blend mode to get the exact static content look you want.






share|improve this answer





















  • Thanks for your answer. I succeeded to resolve my problems. The only side effect is that I had to insert some pages from background to the target pdf. (I didn't mentioned to make the problem simple, but there are page range parameters that should be considered) Although I still want kind of normal and descent way (copying fields and placing them), I have to mark your answer as accepted. Thanks again.
    – Piao David
    Nov 20 at 13:20












  • "I still want kind of normal and descent way (copying fields and placing them)" - I'm afraid that is more complex in general than one would like because of possible collisions - there might already be fonts with the same name in the source PDF and in the target PDF default form resources - requiring a rewrite of some form field properties.
    – mkl
    Nov 20 at 14:39










  • It's reasonable. @mkl. Thanks again!
    – Piao David
    Nov 20 at 15:38











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385456%2fhow-to-embed-all-fonts-from-other-pdf-itext-7%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










In a comment you clarified:




the background PDF can be assumed not to have form fields or annotations. I mean we can assume background PDF only contains static content (scanned form) and the front PDF only contains formfields.




In that case the easiest way to implement your method is to add the background as xobject to the form PDF instead of adding the form to the background PDF.



You can simply do that like this:



PdfReader formReader = new PdfReader(front_path);
PdfReader backReader = new PdfReader(back_path);
PdfWriter writer = new PdfWriter(merge_path);

using (PdfDocument source = new PdfDocument(backReader))
using (PdfDocument target = new PdfDocument(formReader, writer))
{
PdfFormXObject xobject = source.GetPage(1).CopyAsFormXObject(target);
PdfPage targetFirstPage = target.GetFirstPage();
PdfStream stream = targetFirstPage.NewContentStreamBefore();
PdfCanvas pdfCanvas = new PdfCanvas(stream, targetFirstPage.GetResources(), target);
Rectangle cropBox = targetFirstPage.GetCropBox();
pdfCanvas.AddXObject(xobject, cropBox.GetX(), cropBox.GetY());
}


Depending on the exact static contents of the background and the form PDF, you might want to use NewContentStreamAfter instead of NewContentStreamBefore or even to use some nifty blend mode to get the exact static content look you want.






share|improve this answer





















  • Thanks for your answer. I succeeded to resolve my problems. The only side effect is that I had to insert some pages from background to the target pdf. (I didn't mentioned to make the problem simple, but there are page range parameters that should be considered) Although I still want kind of normal and descent way (copying fields and placing them), I have to mark your answer as accepted. Thanks again.
    – Piao David
    Nov 20 at 13:20












  • "I still want kind of normal and descent way (copying fields and placing them)" - I'm afraid that is more complex in general than one would like because of possible collisions - there might already be fonts with the same name in the source PDF and in the target PDF default form resources - requiring a rewrite of some form field properties.
    – mkl
    Nov 20 at 14:39










  • It's reasonable. @mkl. Thanks again!
    – Piao David
    Nov 20 at 15:38















up vote
1
down vote



accepted










In a comment you clarified:




the background PDF can be assumed not to have form fields or annotations. I mean we can assume background PDF only contains static content (scanned form) and the front PDF only contains formfields.




In that case the easiest way to implement your method is to add the background as xobject to the form PDF instead of adding the form to the background PDF.



You can simply do that like this:



PdfReader formReader = new PdfReader(front_path);
PdfReader backReader = new PdfReader(back_path);
PdfWriter writer = new PdfWriter(merge_path);

using (PdfDocument source = new PdfDocument(backReader))
using (PdfDocument target = new PdfDocument(formReader, writer))
{
PdfFormXObject xobject = source.GetPage(1).CopyAsFormXObject(target);
PdfPage targetFirstPage = target.GetFirstPage();
PdfStream stream = targetFirstPage.NewContentStreamBefore();
PdfCanvas pdfCanvas = new PdfCanvas(stream, targetFirstPage.GetResources(), target);
Rectangle cropBox = targetFirstPage.GetCropBox();
pdfCanvas.AddXObject(xobject, cropBox.GetX(), cropBox.GetY());
}


Depending on the exact static contents of the background and the form PDF, you might want to use NewContentStreamAfter instead of NewContentStreamBefore or even to use some nifty blend mode to get the exact static content look you want.






share|improve this answer





















  • Thanks for your answer. I succeeded to resolve my problems. The only side effect is that I had to insert some pages from background to the target pdf. (I didn't mentioned to make the problem simple, but there are page range parameters that should be considered) Although I still want kind of normal and descent way (copying fields and placing them), I have to mark your answer as accepted. Thanks again.
    – Piao David
    Nov 20 at 13:20












  • "I still want kind of normal and descent way (copying fields and placing them)" - I'm afraid that is more complex in general than one would like because of possible collisions - there might already be fonts with the same name in the source PDF and in the target PDF default form resources - requiring a rewrite of some form field properties.
    – mkl
    Nov 20 at 14:39










  • It's reasonable. @mkl. Thanks again!
    – Piao David
    Nov 20 at 15:38













up vote
1
down vote



accepted







up vote
1
down vote



accepted






In a comment you clarified:




the background PDF can be assumed not to have form fields or annotations. I mean we can assume background PDF only contains static content (scanned form) and the front PDF only contains formfields.




In that case the easiest way to implement your method is to add the background as xobject to the form PDF instead of adding the form to the background PDF.



You can simply do that like this:



PdfReader formReader = new PdfReader(front_path);
PdfReader backReader = new PdfReader(back_path);
PdfWriter writer = new PdfWriter(merge_path);

using (PdfDocument source = new PdfDocument(backReader))
using (PdfDocument target = new PdfDocument(formReader, writer))
{
PdfFormXObject xobject = source.GetPage(1).CopyAsFormXObject(target);
PdfPage targetFirstPage = target.GetFirstPage();
PdfStream stream = targetFirstPage.NewContentStreamBefore();
PdfCanvas pdfCanvas = new PdfCanvas(stream, targetFirstPage.GetResources(), target);
Rectangle cropBox = targetFirstPage.GetCropBox();
pdfCanvas.AddXObject(xobject, cropBox.GetX(), cropBox.GetY());
}


Depending on the exact static contents of the background and the form PDF, you might want to use NewContentStreamAfter instead of NewContentStreamBefore or even to use some nifty blend mode to get the exact static content look you want.






share|improve this answer












In a comment you clarified:




the background PDF can be assumed not to have form fields or annotations. I mean we can assume background PDF only contains static content (scanned form) and the front PDF only contains formfields.




In that case the easiest way to implement your method is to add the background as xobject to the form PDF instead of adding the form to the background PDF.



You can simply do that like this:



PdfReader formReader = new PdfReader(front_path);
PdfReader backReader = new PdfReader(back_path);
PdfWriter writer = new PdfWriter(merge_path);

using (PdfDocument source = new PdfDocument(backReader))
using (PdfDocument target = new PdfDocument(formReader, writer))
{
PdfFormXObject xobject = source.GetPage(1).CopyAsFormXObject(target);
PdfPage targetFirstPage = target.GetFirstPage();
PdfStream stream = targetFirstPage.NewContentStreamBefore();
PdfCanvas pdfCanvas = new PdfCanvas(stream, targetFirstPage.GetResources(), target);
Rectangle cropBox = targetFirstPage.GetCropBox();
pdfCanvas.AddXObject(xobject, cropBox.GetX(), cropBox.GetY());
}


Depending on the exact static contents of the background and the form PDF, you might want to use NewContentStreamAfter instead of NewContentStreamBefore or even to use some nifty blend mode to get the exact static content look you want.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 11:58









mkl

52.3k1165143




52.3k1165143












  • Thanks for your answer. I succeeded to resolve my problems. The only side effect is that I had to insert some pages from background to the target pdf. (I didn't mentioned to make the problem simple, but there are page range parameters that should be considered) Although I still want kind of normal and descent way (copying fields and placing them), I have to mark your answer as accepted. Thanks again.
    – Piao David
    Nov 20 at 13:20












  • "I still want kind of normal and descent way (copying fields and placing them)" - I'm afraid that is more complex in general than one would like because of possible collisions - there might already be fonts with the same name in the source PDF and in the target PDF default form resources - requiring a rewrite of some form field properties.
    – mkl
    Nov 20 at 14:39










  • It's reasonable. @mkl. Thanks again!
    – Piao David
    Nov 20 at 15:38


















  • Thanks for your answer. I succeeded to resolve my problems. The only side effect is that I had to insert some pages from background to the target pdf. (I didn't mentioned to make the problem simple, but there are page range parameters that should be considered) Although I still want kind of normal and descent way (copying fields and placing them), I have to mark your answer as accepted. Thanks again.
    – Piao David
    Nov 20 at 13:20












  • "I still want kind of normal and descent way (copying fields and placing them)" - I'm afraid that is more complex in general than one would like because of possible collisions - there might already be fonts with the same name in the source PDF and in the target PDF default form resources - requiring a rewrite of some form field properties.
    – mkl
    Nov 20 at 14:39










  • It's reasonable. @mkl. Thanks again!
    – Piao David
    Nov 20 at 15:38
















Thanks for your answer. I succeeded to resolve my problems. The only side effect is that I had to insert some pages from background to the target pdf. (I didn't mentioned to make the problem simple, but there are page range parameters that should be considered) Although I still want kind of normal and descent way (copying fields and placing them), I have to mark your answer as accepted. Thanks again.
– Piao David
Nov 20 at 13:20






Thanks for your answer. I succeeded to resolve my problems. The only side effect is that I had to insert some pages from background to the target pdf. (I didn't mentioned to make the problem simple, but there are page range parameters that should be considered) Although I still want kind of normal and descent way (copying fields and placing them), I have to mark your answer as accepted. Thanks again.
– Piao David
Nov 20 at 13:20














"I still want kind of normal and descent way (copying fields and placing them)" - I'm afraid that is more complex in general than one would like because of possible collisions - there might already be fonts with the same name in the source PDF and in the target PDF default form resources - requiring a rewrite of some form field properties.
– mkl
Nov 20 at 14:39




"I still want kind of normal and descent way (copying fields and placing them)" - I'm afraid that is more complex in general than one would like because of possible collisions - there might already be fonts with the same name in the source PDF and in the target PDF default form resources - requiring a rewrite of some form field properties.
– mkl
Nov 20 at 14:39












It's reasonable. @mkl. Thanks again!
– Piao David
Nov 20 at 15:38




It's reasonable. @mkl. Thanks again!
– Piao David
Nov 20 at 15:38


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385456%2fhow-to-embed-all-fonts-from-other-pdf-itext-7%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft