Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Mar 28, 2019 6:07 pm

I'm trying to apply a rubberstamp in to a document where the first page was rotate so the location of coordinate (0,0) is on the top left corner of the page on the other pages it is locate on the top right corner. How can i do to make sure that I was going to apply the stamp into the same position besides the page orientation?


Thank You
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Fri Mar 29, 2019 6:27 am

Hello,

Thanks for your inquiry.
You can try the following sample code to apply the rubberstamp into the same position. If there is any question, please provide your testing PDF file to help us further investigate it. You could send it to us via email ([email protected]).
Code: Select all
 //load original pdf
 PdfDocument doc = new PdfDocument();
 doc.LoadFromFile(@"test.pdf");
 //create a new pdf
 PdfDocument newPdf = new PdfDocument();
 //load the image
 PdfImage image = PdfImage.FromFile(@"logo.png");
 foreach (PdfPageBase page in doc.Pages)
 {
     PdfPageBase newPage;
     //judge if the page rotated
     if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
         newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
     else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
         newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
     else
         newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
     page.CreateTemplate().Draw(newPage, new PointF(0, 0));
     //add an image stamp
     PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(10, 300), new SizeF(60, 60)));
     PdfAppearance loApprearance = new PdfAppearance(loStamp);
     PdfTemplate template = new PdfTemplate(160, 160);
     template.Graphics.DrawImage(image, 0, 0);
     loApprearance.Normal = template;
     loStamp.Appearance = loApprearance;
     newPage.AnnotationsWidget.Add(loStamp);
 }
 newPdf.SaveToFile("result.pdf",Spire.Pdf.FileFormat.PDF);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1510
Joined: Wed Apr 25, 2018 3:20 am

Tue Apr 02, 2019 1:58 pm

Thanks for your reply, I'm testing it right now.
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Tue Apr 02, 2019 7:25 pm

I did a few changes in the code you sent to me and it works.

Thank You very much
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Wed Apr 03, 2019 1:40 am

Hello,

Glad to hear that! If we can help you with anything else, just feel free to contact us.
Wish you all the best!

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1510
Joined: Wed Apr 25, 2018 3:20 am

Mon May 06, 2019 3:59 pm

Hi,

Today for the first time I tested this code with a PDF file that has bookmark on, I found out the this code is not carrying over the bookmark. Do you have any idea on how to fix it?


Thank you
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Mon May 06, 2019 6:22 pm

Test file attached.
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Tue May 07, 2019 2:52 am

Hello,

Thank you for contacting.
I regret to tell you that our Spire.PDF doesn't support keeping the bookmarks by page.CreateTemplate().Draw(newPage, new PointF(0, 0)) method above at present.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1510
Joined: Wed Apr 25, 2018 3:20 am

Tue May 07, 2019 12:59 pm

Hi,

There is any other way to deal with the page orientation?I've been doing a few more tests what I found out was that the bookmark is removed in that part of the code:
PdfPageBase newPage;
                     if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
                         newPage = newPdf.Pages.Add (new SizeF (page.Size.Height, page.Size.Width), new PdfMargins (0));
                     else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
                         newPage = newPdf.Pages.Add (new SizeF (page.Size.Height, page.Size.Width), new PdfMargins (0));
                     else
                         newPage = newPdf.Pages.Add (page.Size, new PdfMargins (0));
                     page.CreateTemplate () .Draw (newPage, new PointF (0, 0));

If I just load the PDF file and add a note and then save it, the bookmark stays there.

Thank You
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Wed May 08, 2019 6:35 am

Hello,

Sorry there is no other way to deal with the page orientation at present. Our Spire.PDF provides the following method to copy bookmarks from one PDF to another PDF. Testing my sample PDF (see attachment) with bookmarks has no problem. However, the bookmarks in your PDF (8BKM0060-011 DWG.pdf) have particularity, they don't have actions, that leads to no connection between PDF contents and the bookmarks after copying. I have posted your PDF to our Dev team, they will analyze it further and investigate if there is any solution for you. Sorry for the inconvenience caused.
Code: Select all
//load original pdf
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("MySample.pdf");
//create a new pdf
PdfDocument newPdf = new PdfDocument();
foreach (PdfPageBase page in doc.Pages)
{
    PdfPageBase newPage;
    if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
        newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
    else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
        newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
    else
        newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
    //draw original page to new page
    page.CreateTemplate().Draw(newPage, new PointF(0, 0));
}
//get bookmarks from original pdf
var bms = doc.Bookmarks;
for (int i = 0; i < bms.Count; i++)
{
    //add parent bookmark
    PdfDestination vendorBookmarkDest = new PdfDestination(bms[i].Destination.Page, bms[i].Destination.Location);
    PdfBookmark vendorBookmark = newPdf.Bookmarks.Add(bms[i].Title);
    vendorBookmark.Color = bms[i].Color;
    vendorBookmark.DisplayStyle = bms[i].DisplayStyle;
    vendorBookmark.Action = new PdfGoToAction(vendorBookmarkDest);

    for (int j = 0; j < bms[i].Count; j++)
    {
        //add child bookmark
        PdfDestination partBookmarkDest = new PdfDestination(bms[i][j].Destination.Page, bms[i][j].Destination.Location);
        PdfBookmark partBookmark = vendorBookmark.Add(bms[i][j].Title);
        partBookmark.Color = bms[i][j].Color;
        partBookmark.DisplayStyle = bms[i][j].DisplayStyle;
        partBookmark.Action = new PdfGoToAction(partBookmarkDest);
    }
}
//save to file
newPdf.SaveToFile("result.pdf", FileFormat.PDF);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Fri May 24, 2019 8:57 am

Hello,

Thanks for your patient waiting.
We just released the Spire.PDF Pack(Hot Fix) Version:5.5.21 which contains the fixes of your issue. The bookmarks of your PDF could be copied successfully now. Please download the hotfix from the following link and refer to the following sample code.
Website link: https://www.e-iceblue.com/Download/download-pdf-for-net-now.html
Nuget link: https://www.nuget.org/packages/Spire.PDF/5.5.21
Code: Select all
//load original pdf
 PdfDocument doc = new PdfDocument();
 doc.LoadFromFile(@"8BKM0060-011 DWG.pdf");
 //create a new pdf
 PdfDocument newPdf = new PdfDocument();
 foreach (PdfPageBase page in doc.Pages)
 {
     PdfPageBase newPage;
     if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
         newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
     else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
         newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
     else
         newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));

     page.CreateTemplate().Draw(newPage, new PointF(0, 0));

     var bms = doc.Bookmarks;
     for (int i = 0; i < bms.Count; i++)
     {
         //add parent bookmark
         PdfDestination vendorBookmarkDest = bms[i].Destination;
         vendorBookmarkDest.Page = newPage;
         PdfBookmark vendorBookmark = newPdf.Bookmarks.Add(bms[i].Title);
         vendorBookmark.Color = bms[i].Color;
         vendorBookmark.DisplayStyle = bms[i].DisplayStyle;
         vendorBookmark.Action = new PdfGoToAction(vendorBookmarkDest);
         for (int j = 0; j < bms[i].Count; j++)
         {
             //add child bookmark
             PdfDestination partBookmarkDest = bms[i][j].Destination;
             partBookmarkDest.Page = newPage;
             PdfBookmark partBookmark = vendorBookmark.Add(bms[i][j].Title);
             partBookmark.Color = bms[i][j].Color;
             partBookmark.DisplayStyle = bms[i][j].DisplayStyle;
             partBookmark.Action = new PdfGoToAction(partBookmarkDest);
         }
     }
 }
 newPdf.SaveToFile(@"final.pdf", FileFormat.PDF);

Sincerely,
Nina
E-icbelue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Tue May 28, 2019 7:00 pm

Hi Nina,

I tested the piece of code that you posted, It is duplicating the Bookmarks end not all of the links are working after processed the PDF file.

foreach (PdfPageBase page in document.Pages)
{
PdfPageBase newPage;
if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
else
newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
page.CreateTemplate().Draw(newPage, new PointF(0, 0));

//get bookmarks from original pdf
var bms = document.Bookmarks;

for (int w = 0; w < bms.Count; w++)
{
//Add parent bookmark
//PdfDestination vendorBookmarkDest = new PdfDestination(bms[w].Destination.Page, bms[w].Destination.Location);
PdfDestination vendorBookmarkDest = bms[w].Destination;

vendorBookmarkDest.Page = newPage;
PdfBookmark vendorBookmark = newPdf.Bookmarks.Add(bms[w].Title);
vendorBookmark.Color = bms[w].Color;
vendorBookmark.DisplayStyle = bms[w].DisplayStyle;
vendorBookmark.Action = new PdfGoToAction(vendorBookmarkDest);

for (int j = 0; j < bms[w].Count; j++)
{
//Add child bookmark
//PdfDestination partBookmarkDest = new PdfDestination(bms[w][j].Destination.Page, bms[w][j].Destination.Location);
PdfDestination partBookmarkDest = bms[w][j].Destination;
partBookmarkDest.Page = newPage;
PdfBookmark partBookmark = vendorBookmark.Add(bms[w][j].Title);
partBookmark.Color = bms[w][j].Color;
partBookmark.DisplayStyle = bms[w][j].DisplayStyle;
partBookmark.Action = new PdfGoToAction(partBookmarkDest);
}
}


Take a look in the attached files.

Thank You
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Tue May 28, 2019 8:39 pm

I just figured out that if the PDF file has only one page the code works just file.
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Wed May 29, 2019 3:07 am

Hi,

Thanks for your feedback.
I have reproduced the issue you mentioned and posted it to our Dev team for investigating and fixing. If it is fixed or there is any update, I will let you know. Sorry for the inconvenience caused.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Thu May 30, 2019 1:55 pm

Thanks for the support. Looking forward for the solution.
Thank You

[email protected]
 
Posts: 57
Joined: Tue Mar 19, 2019 6:41 pm

Return to Spire.PDF