I can't seem to get PdfSharp to show a picture for this annotation. It doesn't have the PdfAnnotation.Icon property, so I can't set that.
XFont font = new XFont("Verdana", 10);
PdfPage page = wDoc.Parent.Page;
XGraphics gfx = wDoc.Parent.gfx;
XRect rec = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(30, 60), new XSize(30, 30)));
PdfRectangle rect = new PdfRectangle(rec);
PdfLinkAnnotation link = PdfLinkAnnotation.CreateFileLink(rect, wDoc.FileLocation);
gfx.DrawString("These files were attached:", font, XBrushes.Black, 30, 50, XStringFormat.Default);
link.Rectangle = new PdfRectangle(rec);
page.Annotations.Add(link);
I've gotten it that far, and the annotation DOES exist, except it's a blank box! How do I go about making it say something, or even just show a picture?
You need to use page.AddWebLink(AREArect) and then add the text area with gfx.drawstring(AREArect)
sample code for Uri usage:
...
PdfDocument pdfDoc = PdfReader.Open(myUri.LocalPath, PdfDocumentOpenMode.Import);
PdfDocument pdfNewDoc = new PdfDocument();
for (int i = 0; i < pdfDoc.Pages.Count; i++)
{
PdfPage page = pdfNewDoc.AddPage(pdfDoc.Pages[i]);
XFont fontNormal = new XFont("Calibri", 10, XFontStyle.Regular);
XGraphics gfx = XGraphics.FromPdfPage(page);
var xrect = new XRect(240, 395, 300, 20);
var rect = gfx.Transformer.WorldToDefaultPage(xrect);
var pdfrect = new PdfRectangle(rect);
//file link
page.AddFileLink(pdfrect, myUri.LocalPath);
//web link
//page.AddWebLink(pdfrect, myUri.AbsoluteUri);
gfx.DrawString("MyFileName", fontNormal, XBrushes.Black, xrect, XStringFormats.TopLeft);
}
pdfNewDoc.Save(myDestinationUri.LocalPath + "MyNewPdfFile.pdf");
...
I am not familiar with class PdfLinkAnnotation.
You can use page.AddDocumentLink, page.AddWebLink, and page.AddFileLink to create links.
If you use these methods, you can draw the icon as an image.
Related
When saving, the image has the right design but the PDF has the wrong text on it. Could you explain why the documents are different?
I'm also open to other solutions for saving a PDF of the whole document and the ability to print a selected page of a multi-page document.
thanks :)
EDIT: the image is showing the date ("24th May 2016") which is correct and what I want the PDF to show, but instead the PDF is showing "TEST TEST"
1
public static void pdf() {
DateTime now = DateTime.Now;
string filename = "MixMigraDocAndPdfSharp.pdf";
filename = Guid.NewGuid().ToString("D").ToUpper() + ".pdf";
PdfDocument document = new PdfDocument();
SamplePage1(document);
document.Save(filename);
Process.Start(filename);
}
2
static void SamplePage1(PdfDocument document) {
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.MUH = PdfFontEncoding.Unicode;
gfx.MFEH = PdfFontEmbedding.Default;
XFont font = new XFont("Verdana", 13, XFontStyle.Bold);
gfx.DrawString("TEST TEST", font, XBrushes.Black,
new XRect(100, 100, page.Width - 200, 300), XStringFormats.Center);
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para = sec.AddParagraph();
header("24th May 2016");
DocumentRenderer docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();
docRenderer.RenderObject(gfx, XUnit.FromCentimeter(5), XUnit.FromCentimeter(10), "12cm", para);
PageInfo info = docRenderer.FormattedDocument.GetPageInfo(1);
int dpi = 150;
int dx, dy;
if (info.Orientation == PdfSharp.PageOrientation.Portrait) {
dx = (int)(info.Width.Inch * dpi);
dy = (int)(info.Height.Inch * dpi);
} else {
dx = (int)(info.Height.Inch * dpi);
dy = (int)(info.Width.Inch * dpi);
}
Image image = new Bitmap(dx, dy, PixelFormat.Format32bppRgb);
Graphics graphics = Graphics.FromImage(image);
graphics.Clear(System.Drawing.Color.White);
float scale = dpi / 72f;
graphics.ScaleTransform(scale, scale);
gfx = XGraphics.FromGraphics(graphics, new XSize(info.Width.Point, info.Height.Point));
docRenderer.RenderPage(gfx, 1);
gfx.Dispose();
image.Save("test.png", ImageFormat.Png);
doc.BindToRenderer(docRenderer);
docRenderer.RenderObject(gfx, XUnit.FromCentimeter(5), XUnit.FromCentimeter(10), "12cm", para);
Process.Start("mspaint", "test.png");
}
3
public static void header(String date) {
Paragraph paragraph = new Paragraph();
var dateIssued = firstPage.AddTextFrame();
dateIssued.Height = "1.0cm";
dateIssued.Width = "6.0cm";
dateIssued.Left = "2.1cm";
dateIssued.RelativeHorizontal = RelativeHorizontal.Margin;
dateIssued.Top = "3.55cm";
dateIssued.RelativeVertical = RelativeVertical.Page;
paragraph = dateIssued.AddParagraph(date);
}
You call docRenderer.RenderPage(gfx, 1); for the image only. This renders the header.
You do not call docRenderer.RenderPage(gfx, 1); for the PDF. So no date there, just the "TEST TEST" you draw earlier.
Using the following code, I am able to write page numbers to SOME files.
var outputDocument = new PdfDocument();
var currentPageNumber = 1;
var inputDocument = PdfReader.Open(#"C:\SamplePdfs\display.pdf", PdfDocumentOpenMode.Import);
for (var i = 0; i < inputDocument.PageCount; i++)
{
PdfPage page = outputDocument.AddPage(inputDocument.Pages[i]);
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Arial", 100, XFontStyle.Regular);
gfx.DrawString(Convert.ToString(currentPageNumber), font, XBrushes.Black, new XRect(0, 0, page.Width - 100, page.Height - 100), XStringFormats.BottomCenter);
currentPageNumber++;
}
outputDocument.Save(#"c:\test\sample1.pdf");
Why would I not be able to use that code to write to certain pdfs?
Update: I have found a pdf online that exhibits the same issue.
PDF
Update #2: Different host:
Dropbox
https://www.dropbox.com/s/0e0exs0lhytx3xl/ThirdConversion.pdf?dl=0
The file NewDisplay.pdf uses negative values in the MediaBox: MediaBox[-89.5 -702.5 702.5 -90.5]
Most PDF files use positive values. Maybe your page numbers will show up when you use negative values for x and y.
I want to create a custom page size which is (5"X2") PDF using iTextSharp in C#. Is there any way to do this?
Document doc = new Document(iTextSharp.text.PageSize.A4, 15, 15, 0, 0);
Below code will demonstrate how to implement the custom PDF using PDF coordinates in C#.net. For this task you must aware about the pdf coordinates.
BaseFont f_cn;
string poath = Server.MapPath("~/fonts/CALIBRI.TTF");
f_cn = BaseFont.CreateFont(poath, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
using (System.IO.FileStream fs = new FileStream(Server.MapPath("~/TempPdf") + "\\" + "download.pdf", FileMode.Create))
{
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
Paragraph p = new Paragraph();
// Add meta information to the document
document.AddAuthor("Mikael Blomquist");
document.AddCreator("Sample application using iTestSharp");
document.AddKeywords("PDF tutorial education");
document.AddSubject("Document subject - Describing the steps creating a PDF document");
document.AddTitle("The document title - Amplified Resource Group");
// Open the document to enable you to write to the document
document.Open();
// Makes it possible to add text to a specific place in the document using
// a X & Y placement syntax.
PdfContentByte cb = writer.DirectContent;
cb.SetFontAndSize(f_cb, 16);
// First we must activate writing
cb.BeginText();
// Add an image to a fixed position from disk
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/arg.png"));
png.SetAbsolutePosition(200, 738);
cb.AddImage(png);
writeText(cb, "Header", 30, 718, f_cb, 14);
}
private void writeText(PdfContentByte cb, string Text, int X, int Y, BaseFont font, int Size)
{
cb.SetFontAndSize(font, Size);
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, Text, X, Y, 0);
}
The document also accept the rectangle type, so we can create a rectangle of our custom size like below.
var pgSize = new iTextSharp.text.Rectangle(1000, 1000);
Document document = new Document(pgSize, 5f, 5f, 20f, 20f);
I'm dynamically generating image from text, and existing image on my asp.net website.
Here is the code:
protected void Button1_Click(object sender, EventArgs e)
{
var tytul = Request.QueryString["Tytul"];
var tresc = Request.QueryString["Tresc"];
var font = new Font("Verdana", 23);
var brushForeColor = new SolidBrush(Color.Black);
var brushBackColor = new SolidBrush(Color.FromArgb(248, 247, 182));
var test = new Bitmap(450, 60);
var graphicstest = Graphics.FromImage(test);
var width = (int)graphicstest.MeasureString(tresc, font).Width;
var height = (int)graphicstest.MeasureString(tresc, font).Height;
while (width > 450)
{
height = height + 25;
width = width - 450;
}
var heightall = height + 40 + 30 + 100;
var bitmap = new Bitmap(450, heightall);
var graphics = Graphics.FromImage(bitmap);
var displayRectangle = new Rectangle(new Point(0, 0), new Size(450, 40));
graphics.FillRectangle(brushBackColor, displayRectangle);
//Define string format
var format1 = new StringFormat(StringFormatFlags.NoClip);
format1.Alignment = StringAlignment.Center;
var format2 = new StringFormat(format1);
//Draw text string using the text format
graphics.DrawString(tytul, font, brushForeColor, displayRectangle, format2);
// Rysowanie drugiego boxa
brushBackColor = new SolidBrush(Color.FromArgb(253, 253, 202));
font = new Font("Verdana", 18);
displayRectangle = new Rectangle(new Point(0, 40), new Size(450, height + 30));
graphics.FillRectangle(brushBackColor, displayRectangle);
displayRectangle = new Rectangle(new Point(0, 55), new Size(450, height + 15));
graphics.DrawString(tresc, font, brushForeColor, displayRectangle, format2);
graphics.DrawImage(System.Drawing.Image.FromFile(Server.MapPath(".") + "/gfx/layout/podpis.png"), new Point(0, height + 70));
Response.ContentType = "image/png";
bitmap.Save(Response.OutputStream, ImageFormat.Png);
}
As you can see the bitmap is saved and showed on aspx page after postback. What I wanna do is when user click Button1, then image is generated and browser download window pops up, without saving on server or showing on page. How to do this? Please help me.
Cheers.
You need to add a Content-Disposition header.
After you save the file:
Response.AppendHeader("content-disposition", "attachment; filename=podpis.png" );
Response.WriteFile("yourfilepath/podpis.png");
Response.End;
Hi there
i had been sucessfully using this great library PDF Sharp.now i wanted to play with some dynamic Stuff so people recomended to switch to Migradoc I did and like its paragraph feature.Now the problem is that when i add long paragraph then new page is not added instead of that there is incomplete text shown(incomplete in sense that text overflows) and i have added a image at the bottom for footer look.how can i do that i enter dynamic text (variable length ) and it just adds the required Number of pages.
My code so far is
XFont font = new XFont("Times New Roman", 12, XFontStyle.Bold);
XFont fontReg = new XFont("Times New Roman", 12, XFontStyle.Regular);
// HACKĀ²
gfx.MUH = PdfFontEncoding.Unicode;
gfx.MFEH = PdfFontEmbedding.Default;
string appPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
XImage image = XImage.FromFile(appPath + "/header.png");
gfx.DrawImage(image, 0, 0);
//Civil Stamp
gfx.DrawImage(XImage.FromFile(appPath + "/cStamp.png"), 370, 380);
gfx.DrawImage(XImage.FromFile(appPath + "/Sp.png"), 230, 380);
CoverPageHeader();
Document doc = new Document();
MigraDoc.DocumentObjectModel.Section sec = doc.AddSection();
// Add a single paragraph with some text and format information.
MigraDoc.DocumentObjectModel.Paragraph para = sec.AddParagraph();
para.Format.Alignment = ParagraphAlignment.Left;
para.Format.Font.Name = "Times New Roman";
para.Format.Font.Size = 12;
para.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.Black;
para.AddText("We are pleased to present the attached Document Please review the Agreement and, if acceptable, " +
"sign one copy and return it to us. We will sign the copy of the agreement and return one for " +
"your records.");
para.AddLineBreak();
para.AddLineBreak();
para.AddText(longlongtextString);
para.AddLineBreak();
para.AddLineBreak();
para.AddText("Sincerely,");
MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();
// Render the paragraph. You can render tables or shapes the same way.
docRenderer.RenderObject(gfx, XUnit.FromCentimeter(0.7), XUnit.FromCentimeter(9), "18cm", para);
gfx.DrawString("Kelly Turbin PhD., P.E.-SECB", font, XBrushes.Black, 20, 500);
gfx.DrawString("Principal", font, XBrushes.Black, 20, 520);
gfx.DrawString("Project No " + textBoxProjNumber.Text, fontReg, XBrushes.Black, 20,785);
gfx.DrawImage(XImage.FromFile(appPath + "/AccB.png"), 20, 700);
gfx.DrawImage(XImage.FromFile(appPath + "/ScreenMagic.png"), 100, 690);
gfx.DrawImage(XImage.FromFile(appPath + "/Footer.png"), 220, 750);
}
Don't use RenderObject, instead use RenderDocument and pages will be created automatically as needed.
Sample code here:
http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx
There is no way to automatically create new pages based on text size, when you are using the Mix of PDFSharp and MigraDoc, as it is in the question. The only solution is to use only MigraDoc with its RenderDocument method.